package cn.gailvlun.gll.widget.dialog;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import com.bigkoo.pickerview.adapter.NumericWheelAdapter;
import com.bigkoo.pickerview.lib.WheelView;

import org.joda.time.DateTime;

import cn.gailvlun.gll.net.R;



public class TimePicker extends LinearLayout {
    public TimePicker(Context context) {
        this(context, null);
    }

    public TimePicker(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TimePicker(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        inflate(getContext(), R.layout.picker_time, this);
        hour = findViewById(R.id.hour);
        minute = findViewById(R.id.minute);
    }

    private WheelView hour;
    private WheelView minute;

    private NumericWheelAdapter hourAdapter;
    private NumericWheelAdapter minuteAdapter;

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();

        if (isInEditMode()) {
            setRange(6, 11);
        }
    }

    public void setFix(DateTime dateTime) {
        int hour = dateTime.getHourOfDay();
        int minute = dateTime.getMinuteOfHour();
        hourAdapter = new NumericWheelAdapter(hour, hour);
        minuteAdapter = new NumericWheelAdapter(minute, minute);

        this.hour.setAdapter(hourAdapter);
        this.hour.setCyclic(false);
        this.hour.setTextSize(14);
        this.minute.setAdapter(minuteAdapter);
        this.minute.setCyclic(false);
        this.minute.setTextSize(14);
    }

    private Integer mStartHour;
    private Integer mEndHour;

    
    public void setRange(int startHour, int endHour) {
        mStartHour = startHour;
        mEndHour = endHour;

        hourAdapter = new NumericWheelAdapter(startHour, endHour);
        minuteAdapter = new NumericWheelAdapter(0, 59);

        this.hour.setAdapter(hourAdapter);
        this.hour.setCyclic(false);
        this.hour.setTextSize(14);
        this.minute.setAdapter(minuteAdapter);
        this.minute.setCyclic(false);
        this.minute.setTextSize(14);
        this.minute.setCurrentItem(0);
    }

    public void setRangeHourFixMinute(int startHour, int endHour, int fixTime) {
        mStartHour = startHour;
        mEndHour = endHour;

        hourAdapter = new NumericWheelAdapter(startHour, endHour);
        minuteAdapter = new NumericWheelAdapter(fixTime, fixTime);

        this.hour.setAdapter(hourAdapter);
        this.hour.setCyclic(false);
        this.hour.setTextSize(14);
        this.minute.setAdapter(minuteAdapter);
        this.minute.setCyclic(false);
        this.minute.setTextSize(14);
        this.minute.setCurrentItem(0);
    }

    private int mSelectHour;


    public void setSelectTime(int selectHour) {
        mSelectHour = selectHour;

        if (mStartHour == null || mEndHour == null) {
            return;
        }
        if (selectHour >= mStartHour && selectHour <= mEndHour) {
            hour.setCurrentItem(hourAdapter.indexOf(selectHour));
        }
    }

    public DateTime getTime() {
        return DateTime.now()
                .withHourOfDay((Integer) hourAdapter.getItem(hour.getCurrentItem()))
                .withMinuteOfHour((Integer) minuteAdapter.getItem(minute.getCurrentItem()));
    }
}
